#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "platform.h"
#include "sj_print.h"
#include "sys_stdio.h"
#include "kernel.h"
#include "os_app_hooks.h"
#include "main_task.h"
#include "gui_task.h"
#include "control_task.h"
#include "input_task.h"
#include "task_manager.h"
#include "sys_operater.h"

typedef struct ICCardInfo {
   unsigned char reserved[32];//姓名
   unsigned char driver_id[18];//驾驶员代码
   unsigned char valid_year;//年
   unsigned char valid_month;//月
   unsigned char valid_day;//日
   unsigned char quali_certificate[18];//机动车驾驶证号
   unsigned char standed_extern[56];//从业资格证号
   unsigned char checksum;
   unsigned char status;
}__attribute__((aligned(1))) ICCardInfo;

static ICCardInfo datatemp;

static TASK_MODULE_DATA(main,MAIN_TASK_PRIO,MAIN_STK_SIZE);

struct sj_task_t main_task_handle = {0};

void main_task(void *arg);

void sanji_main(void)
{
    CPU_SR_ALLOC();
    sj_int8 ret = -1;
    sj_platform_initcall();

/*
    int fd_i2c = sj_open("/dev/i2c", 0);
    char reg = 0x0;
    sj_ioctl(fd_i2c, 0, &reg);
    sj_uchar data[256] = "";

    sj_read(fd_i2c, (sj_uint8 *)&datatemp, sizeof(ICCardInfo));

    sj_printf(" [slj]read data: %s\r\n", datatemp.reserved);
*/

    main_task_handle.name="main";
    main_task_handle.run_func = main_task;
    GET_TASK_MODULE_DATA(main_task_handle.task_data,main);
    main_task_handle.task_data->periodtime=0;

    os_init();
    CPU_Init();
    OS_CRITICAL_ENTER();

    sj_task_create(&main_task_handle);

    OS_CRITICAL_EXIT();	//退出临界区
    os_start();
    return ;
}

void main_task(void *arg)
{
    struct control_init_param param= {0};
    struct input_init_param param_input = {0};

    sj_kprintf("enter main_task\r\n");
    OS_ERR err;
    CPU_SR_ALLOC();
    OS_CRITICAL_ENTER();

    param.gui_run = gui_task_run_now;
    control_task_init(&param);

    gui_task_init();

    param_input.control_run = control_task_input_type;
    input_task_init(&param_input);

    OS_CRITICAL_EXIT();	//退出临界区
    main_task_handle.task_data->exit = 1;
}


